import 'package:flutter/material.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/bussine_font.dart';
import 'package:honeypotflutter/publicBussine/bussine_image.dart';

class MGSearch extends StatelessWidget {
  /// 提示
  final String? hintText;

  /// 聚焦
  final bool autofocus;

  /// 改变
  final Function(String value)? onChanged;

  /// 按钮
  final Function(String value)? onClick;

  MGSearch({
    super.key,
    this.hintText,
    this.onChanged,
    this.onClick,
    this.autofocus = false,
  });

  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Container(
      color: MGColors.whileColor,
      padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
      child: Container(
        decoration: BoxDecoration(
          color: MGColors.backColor,
          borderRadius: BorderRadius.circular(6),
        ),
        padding: EdgeInsets.only(left: 16),
        child: Row(
          children: [
            Image.asset(MGImage.image("public_search", type: ImagePathType.public), width: 20),
            SizedBox(width: 8),
            Expanded(
                child: TextField(
              controller: _controller,
              autofocus: autofocus,
              style: TextStyle(
                color: MGColors.text1Color,
                fontSize: MGFont.textFont1,
              ),
              textInputAction: TextInputAction.search,
              decoration: InputDecoration(
                hintText: hintText,
                hintStyle: TextStyle(
                  color: MGColors.text3Color,
                  fontSize: MGFont.textFont1,
                ),
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.transparent),
                ),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.transparent),
                ),
              ),
              onChanged: onChanged,
              onSubmitted: onClick,
            )),
            SizedBox(width: 8),
            Row(
              children: [
                Container(
                  color: MGColors.text3Color,
                  width: 3,
                  height: 15,
                ),
                TextButton(
                  onPressed: () {
                    if (onClick != null) {
                      onClick!(_controller.text);
                      FocusManager.instance.primaryFocus?.unfocus();
                    }
                  },
                  child: Text(
                    "搜索",
                    style: TextStyle(
                      fontSize: MGFont.textFont2,
                      color: MGColors.text1Color,
                    ),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}
